package com.androway.domains;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.google.gson.annotations.Expose;

public class Group extends AbstractPersistentObject implements Serializable
{
	private static final long	serialVersionUID	= 1L;

	public static int			LEVEL_GOD			= -2;
	public static int			LEVEL_NONE			= -1;

	@Expose
	private String				name;
	@Expose
	private String				description;
	@Expose
	private int					level;
	@Expose
	private boolean				hidden;
	@Expose
	private Set<Permission>		permissionList		= new HashSet<Permission>();
	@Expose
	private Group				parent;
	private School				school;

	public boolean hasPermission(String name, int value)
	{
		// TODO Change the way the inheritance is handled: if the son have a service user it's value and do search a flag in it's parent
		for (Permission perm : this.permissionList)
			if (perm.getName().equals(name) || (perm.getName().equals(Service.ALL) && !name.equals(Service.Type)))
				if ((perm.getValue() & value) != 0 || perm.getValue() == Permission.ALL)
					return true;
		if (this.parent != null)
			return this.parent.hasPermission(name, value);
		return false;
	}

	public void addPermission(String name, int value)
	{
		for (Permission perm : this.permissionList)
			if (perm.getName().equals(name))
			{
				perm.setValue(perm.getValue() | value);
				return;
			}
		Permission perm = new Permission();
		perm.setName(name);
		perm.setValue(value);
		this.permissionList.add(perm);
	}

	public String getName()
	{
		return this.name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getDescription()
	{
		return this.description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public int getHigherLevel()
	{
		if (this.level == Group.LEVEL_GOD || this.level != Group.LEVEL_NONE || this.parent == null)
			return this.level;
		int higherLevel = this.level;
		int parentLevel = this.parent.getHigherLevel();
		if (parentLevel > higherLevel)
			higherLevel = parentLevel;
		return higherLevel;
	}

	public int getLevel()
	{
		return this.level;
	}

	public void setLevel(int level)
	{
		this.level = level;
	}

	public boolean isHidden()
	{
		return this.hidden;
	}

	public void setHidden(boolean hidden)
	{
		this.hidden = hidden;
	}

	public Set<Permission> getPermissionList()
	{
		return this.permissionList;
	}

	public void setPermissionList(Set<Permission> permissionList)
	{
		this.permissionList = permissionList;
	}

	public Group getParent()
	{
		return this.parent;
	}

	public void setParent(Group parent)
	{
		this.parent = parent;
	}

	public School getSchool()
	{
		return this.school;
	}

	public void setSchool(School school)
	{
		this.school = school;
	}

	@Override
	public String toString()
	{
		return this.getName() + "( " + this.getUid() + ")";
	}
}